package com.tensquare.qa.controller;

import com.tensquare.qa.client.BaseClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 控制器层.
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {

  @Autowired private ProblemService problemService;
  @Resource private BaseClient baseClient;

  /** 使用Feign调用Label服务Demo. */
  @GetMapping("/label/{labelId}")
  public Result findByLabelId(@PathVariable("labelId") String labelId) {
    return baseClient.findById(labelId);
  }

  /** 最新回答. */
  @GetMapping("/newlist/{labelId}/{page}/{size}")
  public Result newList(
      @PathVariable String labelId, @PathVariable int page, @PathVariable int size) {
    Page<Problem> pageData = problemService.newList(labelId, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
  }

  /** 最新回答. */
  @GetMapping("/hotlist/{labelId}/{page}/{size}")
  public Result hotList(
      @PathVariable String labelId, @PathVariable int page, @PathVariable int size) {
    Page<Problem> pageData = problemService.hotList(labelId, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
  }

  /** 等待回答. */
  @GetMapping("/waitlist/{labelId}/{page}/{size}")
  public Result waitlist(
      @PathVariable String labelId, @PathVariable int page, @PathVariable int size) {
    Page<Problem> pageData = problemService.waitList(labelId, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
  }

  /**
   * 增加
   *
   * @param problem
   */
  @RequestMapping(method = RequestMethod.POST)
  public Result add(@RequestBody Problem problem, HttpServletRequest request) {
    String token = (String) request.getAttribute("claims_user");
    if (StringUtils.isBlank(token)) {
      return new Result(false, StatusCode.ACCESSERROR, "权限不足");
    }
    problemService.add(problem);
    return new Result(true, StatusCode.OK, "增加成功");
  }

  /**
   * 查询全部数据
   *
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  public Result findAll() {
    return new Result(true, StatusCode.OK, "查询成功", problemService.findAll());
  }

  /**
   * 根据ID查询
   *
   * @param id ID
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Result findById(@PathVariable String id) {
    return new Result(true, StatusCode.OK, "查询成功", problemService.findById(id));
  }

  /**
   * 分页+多条件查询
   *
   * @param searchMap 查询条件封装
   * @param page 页码
   * @param size 页大小
   * @return 分页结果
   */
  @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
  public Result findSearch(
      @RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
    Page<Problem> pageList = problemService.findSearch(searchMap, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent()));
  }

  /**
   * 根据条件查询
   *
   * @param searchMap
   * @return
   */
  @RequestMapping(value = "/search", method = RequestMethod.POST)
  public Result findSearch(@RequestBody Map searchMap) {
    return new Result(true, StatusCode.OK, "查询成功", problemService.findSearch(searchMap));
  }

  /**
   * 修改
   *
   * @param problem
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  public Result update(@RequestBody Problem problem, @PathVariable String id) {
    problem.setId(id);
    problemService.update(problem);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /**
   * 删除
   *
   * @param id
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public Result delete(@PathVariable String id) {
    problemService.deleteById(id);
    return new Result(true, StatusCode.OK, "删除成功");
  }
}
